package com.nari.ncps.screen.service.impl.electric;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.electric.ElectricSafetyenvDensityRt;
import com.nari.ncps.screen.mapper.electric.ElectricSafetyenvDensityRtMapper;
import com.nari.ncps.screen.service.electric.ElectricSafetyenvDensityRtService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 火电安全环保浓度实时数据表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-10-12
 */
@Service
public class ElectricSafetyenvDensityRtServiceImpl extends ServiceImpl<ElectricSafetyenvDensityRtMapper, ElectricSafetyenvDensityRt> implements ElectricSafetyenvDensityRtService {

    @Override
    public List<ElectricSafetyenvDensityRt> getByYear(Integer year) {
        return baseMapper.selectByYear(year);
    }

    @Override
    public List<ElectricSafetyenvDensityRt> getByYearAndMonth(Integer year, Integer month) {
        return baseMapper.selectByYearAndMonth(year, month);
    }

    @Override
    public List<ElectricSafetyenvDensityRt> getByPlantCode(String plantCode) {
        return baseMapper.selectByPlantCode(plantCode);
    }

    @Override
    public List<ElectricSafetyenvDensityRt> getByYearMonthDay(Integer year, Integer month, Integer day) {
        return baseMapper.selectByYearMonthDay(year, month, day);
    }

    @Override
    public ElectricSafetyenvDensityRt getLatestByPlantCode(String plantCode) {
        return baseMapper.selectLatestByPlantCode(plantCode);
    }

    @Override
    public List<ElectricSafetyenvDensityRt> getDailyTrend(Integer year, Integer month) {
        QueryWrapper<ElectricSafetyenvDensityRt> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year);
        if (month != null) {
            queryWrapper.eq("month", month);
        }
        queryWrapper.orderBy(true, true, "day", "record_date");
        return list(queryWrapper);
    }

    @Override
    public boolean checkExceedStandard(String plantCode) {
        ElectricSafetyenvDensityRt latest = getLatestByPlantCode(plantCode);
        
        if (latest == null) {
            return false;
        }
        
        // 定义标准值（这些值可以根据实际环保标准调整）
        int so2Standard = 35;      // 二氧化硫标准值 mg/m³
        int dustStandard = 10;     // 烟尘标准值 mg/m³
        int nitrogenStandard = 50; // 氮氧化物标准值 mg/m³
        
        // 检查是否有任何一项超标
        boolean exceeds = false;
        
        if (latest.getSo2Amount() != null && latest.getSo2Amount() > so2Standard) {
            exceeds = true;
        }
        
        if (latest.getDustStandard() != null && latest.getDustStandard() > dustStandard) {
            exceeds = true;
        }
        
        if (latest.getNitrogenAmount() != null && latest.getNitrogenAmount() > nitrogenStandard) {
            exceeds = true;
        }
        
        return exceeds;
    }

    @Override
    public ElectricSafetyenvDensityRt getDensityStatistics(Integer year, Integer month) {
        QueryWrapper<ElectricSafetyenvDensityRt> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year);
        if (month != null) {
            queryWrapper.eq("month", month);
        }
        
        List<ElectricSafetyenvDensityRt> dataList = list(queryWrapper);
        
        if (dataList == null || dataList.isEmpty()) {
            return null;
        }
        
        // 计算平均值
        ElectricSafetyenvDensityRt statistics = new ElectricSafetyenvDensityRt();
        statistics.setYear(year);
        statistics.setMonth(month);
        
        int so2Sum = 0;
        int dustSum = 0;
        int nitrogenSum = 0;
        int count = dataList.size();
        
        for (ElectricSafetyenvDensityRt data : dataList) {
            if (data.getSo2Amount() != null) {
                so2Sum += data.getSo2Amount();
            }
            if (data.getDustStandard() != null) {
                dustSum += data.getDustStandard();
            }
            if (data.getNitrogenAmount() != null) {
                nitrogenSum += data.getNitrogenAmount();
            }
        }
        
        // 设置平均值
        statistics.setSo2Amount(so2Sum / count);
        statistics.setDustStandard(dustSum / count);
        statistics.setNitrogenAmount(nitrogenSum / count);
        statistics.setUnit(dataList.get(0).getUnit());
        
        return statistics;
    }
}

